package com.tanguy.user.ui.main

import android.content.Intent
import android.view.View
import com.tanguy.user.App.Companion.app
import com.tanguy.user.Const
import com.tanguy.user.R
import com.tanguy.user.base.LazyFragment
import com.tanguy.user.bean.User
import com.tanguy.user.ui.me.activity.*
import com.tanguy.user.ui.me.activity.account.join.JoinActivity
import com.tanguy.user.ui.me.activity.order.OrderActivity
import com.tanguy.user.ui.me.activity.wallet.WalletActivity
import com.tanguy.user.utlis.extend.click
import kotlinx.android.synthetic.main.fragment_me.*
import org.jetbrains.anko.support.v4.startActivity
import org.jetbrains.anko.support.v4.startActivityForResult
import java.util.*


/**
 * Created by dab on 2017/9/30 0030 11:26
 */
class MeFragment : LazyFragment() {
    override fun viewLayoutID(): Int = R.layout.fragment_me
    override fun onFirstVisibleToUser(view: View?) {
        iv_portrait.setImageURI(app.user.headImg)
        tv_phone.text = app.user.phone
        tv_signature.text = app.user.autograph
        tv_wallet.text = String.format(Locale.CHINA, "%.2f元", app.user.balance)
        tv_coupons.text = "${app.user.num}张"
        view!!.apply {
            click(R.id.iv_portrait) { startActivityForResult<MeMessageActivity>(Const.RequestCode.UP_USER) }//我的消息
            click(R.id.tv_order) { startActivity<OrderActivity>() }//订单
            click(R.id.view_wallet) { startActivity<WalletActivity>() }//钱包
            click(R.id.layout_coupons) { startActivity<CouponsActivity>() }//优惠卷
            click(R.id.tv_grade) { startActivity<GradeActivity>() }//我的评价
            click(R.id.tv_accounting_rules) {}//计费规则
            click(R.id.tv_recommend) { startActivity<RecommendActivity>() }//推荐有礼
            click(R.id.tv_join) { startActivity<JoinActivity>() }//我要加盟
            click(R.id.tv_more) { startActivity<MoreActivity>() }//更多
        }

    }

    /**
     * 刷新Ui
     */
    fun refreshUI(user: User) {
        iv_portrait.setImageURI(user.headImg)
        tv_phone.text = user.phone
        tv_signature.text = user.autograph
        tv_wallet.text = String.format(Locale.CHINA, "%.2f元", user.balance)
        tv_coupons.text = "${user.num}张"
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == Const.RequestCode.UP_USER) {
            val mainActivity = activity as MainActivity
            mainActivity.showMe(app.user)
        }
    }
}